﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entity.Admin;

namespace Service
{
    public class MessageService : BaseService<Message>
    {
        public MessageService(DBEntities db)
            : base(db)
        { }

        public List<Message> GetUserReceivedMessagesByUserName(string userName)
        {
            var user = db.Users.FirstOrDefault(p => p.UserName == userName);
            return GetUserReceivedMessages(user);
        }

        public List<Message> GetUserReceivedMessages(string userId)
        {
            var user = db.Users.Find(userId);
            return GetUserReceivedMessages(user);
        }

        private List<Message> GetUserReceivedMessages(User user)
        {
            if (user != null)
            {
                return user.UserMatchMessages.Where(p => p.Receiver_ID == user.ID).Select(p => p.Message).OrderByDescending(p => p.SendTime).ToList();
            }
            else
            {
                return new List<Message>();
            }

        }

        public List<Message> GetUserSendMessagesByUserName(string userName)
        {
            var user = db.Users.FirstOrDefault(p => p.UserName == userName);
            if (user == null)
            {
                return new List<Message>();
            }
            else
            {
                return GetUserSendMessages(user.ID);
            }

        }

        public List<Message> GetUserSendMessages(string userId)
        {
            return db.Messages.Where(p => p.Sender_ID == userId).OrderByDescending(p => p.SendTime).ToList();
        }

    }
}
